#
# Makefile
#

PREFIX          = @INSTALLPREFIX@
OSTYPE          = @OSTYPE@
UNAME          := $(shell uname)
MACHTYPE	= @MACHTYPE@
PROG		= @PROG@
SOURCES	       := main.cxx video.cxx conference.cxx filemembers.cxx custom.cxx h323.cxx html.cxx mcu.cxx sip.cxx template.cxx \
                   utils.cxx utils_av.cxx utils_list.cxx utils_type.cxx utils_json.cxx yuv.cxx \
                   mcu_rtp.cxx mcu_rtp_cache.cxx mcu_rtp_secure.cxx \
                   sockets.cxx telnet.cxx \
                   reg.cxx reg_sip.cxx reg_h323.cxx rtsp.cxx recorder.cxx mcu_caps.cxx mcu_codecs.cxx

CXX		= g++
CFLAGS         += @CFLAGS@
LDFLAGS        += @MCU_LDFLAGS@
RFLAGS         += @RPATH@
SFLAGS         += @MCU_SFLAGS@
SFLAGS_STATIC  += @MCU_SFLAGS_STATIC@
SFLAGS_DEBUG   += @MCU_SFLAGS_DEBUG@
LDLIBS	       += @MCU_LDLIBS@
LDLIBS_STATIC  += @MCU_LDLIBS_STATIC@
LDLIBS_DEBUG   += @MCU_LDLIBS_DEBUG@
STDCCFLAGS     += @STDCCFLAGS@ -D__STDC_CONSTANT_MACROS -Wall
STDCXXFLAGS    += @STDCXXFLAGS@
CPPFLAGS       += @MCU_CPPFLAGS@
OPTCCFLAGS      =$(CPPFLAGS)

git_revision        = $(shell git rev-list HEAD --max-count=1)
#git_revision_number = $(shell git rev-list HEAD | wc -l)
STDCCFLAGS     +=-D GIT_REVISION=$(git_revision)

PTLIBDIR        = @PTLIBDIR@
include $(PTLIBDIR)/make/ptbuildopts.mak

INSTALL = install
INSTALL_BACKUP_OPTS =
ifeq ($(UNAME), Linux)
  INSTALL = install
  INSTALL_BACKUP_OPTS = --compare --backup=numbered
endif
ifeq ($(UNAME), FreeBSD)
ifeq (, $(shell which ginstall))
  INSTALL = install
  INSTALL_BACKUP_OPTS = -C -b
else
  INSTALL = ginstall
  INSTALL_BACKUP_OPTS = --compare --backup=numbered
endif
endif


OBJDIR	= ./obj
OBJECTS = $(addprefix $(OBJDIR)/,$(patsubst %.c,%.o,$(patsubst %.cxx,%.o,$(notdir $(SOURCES)))))

$(OBJDIR)/%.o : %.cxx
	@mkdir -p $(OBJDIR) >/dev/null 2>&1
	@echo [CC] $@
	@$(CXX) $(STDCCFLAGS) $(OPTCCFLAGS) $(CFLAGS) $(STDCXXFLAGS) -c $< -o $@

all: $(OBJECTS)
	$(CXX) $(LDSO) -o $(OBJDIR)/$(PROG) $^ $(CFLAGS) $(LDFLAGS) $(SFLAGS) $(RFLAGS) $(OBJS) $(LDLIBS) $(ENDLDLIBS) $(ENDLDFLAGS)

static: $(OBJECTS)
	$(CXX) $(LDSO) -o $(OBJDIR)/$(PROG) $^ $(CFLAGS) $(LDFLAGS) $(SFLAGS_STATIC) $(RFLAGS) $(OBJS) $(LDLIBS_STATIC) $(ENDLDLIBS) $(ENDLDFLAGS)

debug: $(OBJECTS)
	$(CXX) $(LDSO) -o $(OBJDIR)/$(PROG) $^ $(CFLAGS) $(LDFLAGS) $(SFLAGS_DEBUG) $(RFLAGS) $(OBJS) $(LDLIBS_DEBUG) $(ENDLDLIBS) $(ENDLDFLAGS)


install:
	mkdir -p $(DESTDIR)@MCU_DIR@
	mkdir -p $(DESTDIR)@MCU_BIN_DIR@
	mkdir -p $(DESTDIR)@MCU_CONFIG_DIR@
	mkdir -p $(DESTDIR)@MCU_FONT_DIR@
	mkdir -p $(DESTDIR)@MCU_LIB_DIR@
	mkdir -p $(DESTDIR)@MCU_LOG_DIR@
	mkdir -p $(DESTDIR)@MCU_PIPE_DIR@
	mkdir -p $(DESTDIR)@MCU_RECORDS_DIR@
	mkdir -p $(DESTDIR)@MCU_RESOURCE_DIR@
	mkdir -p $(DESTDIR)@MCU_SSL_DIR@
	mkdir -p $(DESTDIR)@MCU_DIR@/scripts
	( for file in ./files/conf/*; do \
	    $(INSTALL) -m 644 $(INSTALL_BACKUP_OPTS) $$file $(DESTDIR)@MCU_CONFIG_DIR@; \
	done )
	( for file in ./files/font/*; do \
	    $(INSTALL) -m 644 $$file $(DESTDIR)@MCU_FONT_DIR@; \
	done )
	( for file in ./files/resource/*; do \
	    $(INSTALL) -m 644 $$file $(DESTDIR)@MCU_RESOURCE_DIR@; \
	done )
	( for file in ./files/scripts/*; do \
	    $(INSTALL) -m 755 $(INSTALL_BACKUP_OPTS) $$file $(DESTDIR)@MCU_DIR@/scripts; \
	done )
	$(INSTALL) -m 755 $(OBJDIR)/$(PROG) $(DESTDIR)@MCU_BIN_DIR@
#	ln -sf $(DESTDIR)@MCU_BIN_DIR@/$(PROG) /usr/local/bin/$(PROG)
	$(INSTALL) -m 644 ../AUTHORS $(DESTDIR)@MCU_DIR@
	$(INSTALL) -m 644 ../COPYING $(DESTDIR)@MCU_DIR@
	$(INSTALL) -m 644 ../NEWS $(DESTDIR)@MCU_DIR@
	$(INSTALL) -m 644 ../README $(DESTDIR)@MCU_DIR@

uninstall:
	rm -rf $(DESTDIR)@MCU_FONT_DIR@
	rm -rf $(DESTDIR)@MCU_LOG_DIR@
	rm -rf $(DESTDIR)@MCU_PIPE_DIR@
	rm -rf $(DESTDIR)@MCU_RESOURCE_DIR@
	rm -rf $(DESTDIR)@MCU_DIR@/scripts
	rm -f $(DESTDIR)@MCU_DIR@/AUTHORS
	rm -f $(DESTDIR)@MCU_DIR@/COPYING
	rm -f $(DESTDIR)@MCU_DIR@/NEWS
	rm -f $(DESTDIR)@MCU_DIR@/README
	rm -f $(DESTDIR)@MCU_BIN_DIR@/$(PROG)
#	rm -f /usr/local/bin/$(PROG)

clean:
	rm -rf $(OBJDIR)
